import uniCopy from '@/js_sdk/xb-copy/uni-copy.js'
import store from '../store/index.js'
export default {

	// 上传文件
	async upload(img) {
		return new Promise(async (resolve, reject) => {
			let header = {};
			if (store.state.token) {
				header.token = store.state.token
			}
			uni.uploadFile({
				url: store.state.baseUrl + 'file/uploadFiles',
				filePath: img,
				name: 'file',
				header,
				success(res) {
					let data = JSON.parse(res.data)
					console.log(data)
					if (data.code == 1) {
						resolve(data.data)
					} else {
						reject()
					}
				},
				fail() {
					reject()
				}
			})
		})
	},
	// 复制(兼容三端)
	copy(content) {
		uniCopy({
			content,
			success: (res) => {
				uni.showToast({
					title: res,
					icon: 'none'
				})
			},
			error: (e) => {
				uni.showToast({
					title: e,
					icon: 'none',
					duration: 3000,
				})
			}
		})
	},
	// loading
	loading(title, mask = true) {
		uni.showLoading({
			mask,
			title
		})
	},
	// showtoast
	toast(title, icon = 'none') {
		uni.showToast({
			icon: icon,
			title,
		})
	},
	// 获取设备id
	getId() {
		return new Promise((resolve) => {
			// #ifndef APP-PLUS
			resolve('1008611')
			// #endif
			// #ifdef APP-PLUS
			plus.device.getInfo({
				success: function(e) {
					resolve(e.uuid)
				},
				fail: function(e) {
					resolve('')
				}
			});
			// #endif
		})
	},
	// 截取小数点后四位
	sub(num, len = 4) {
		num = Number(num);
		num = num.toString();
		if (num.indexOf(".") == -1) {
			return Number(num)
		} else {
			return Number(num.substring(0, (num.indexOf(".") + len + 1)));
		}
	},
	Dec(num, len = 9) { /*截取小数点后n位(默认8)并向上取1*/
		num = Number(num);
		num = num.toString();
		if (num.indexOf(".") == -1) {
			return Number(num)
		} else {
			return (Number(num.substring(0, (num.indexOf(".") + len + 1)))).toFixed(8);
		}
	},
}
